package com.hellowx.qydcdev.utils;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hellowx.qydcdev.constant.Constant;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.Random;

public class ImageUtils {
    /**
     *  BufferedImage转成 base64
     * @param bufferedImage
     * @param imageFormatName
     * @return
     * @throws IOException
     */
    public static String getBufferedImageToBase64(BufferedImage bufferedImage, String imageFormatName) throws IOException {
        if(StringUtils.isBlank(imageFormatName)){
            imageFormatName = "png";
        }
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, imageFormatName, stream);
        return Base64.getEncoder().encodeToString(stream.toByteArray());
    }

    public static String randomAvatar(){
        return Constant.AVATARS[new Random().nextInt(Constant.AVATARS.length)];
    }
}
